//
//  HomeDeliciousFoodDetailController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/22.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "HomeDeliciousFoodDetailController.h"
#import "AnswerDetailCommentsCell.h"

@interface HomeDeliciousFoodDetailController ()<UITableViewDelegate,UITableViewDataSource>
/**listView*/
@property (nonatomic,strong)UITableView*listView;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataSoure;
/**底部输入框视图*/
@property (nonatomic,strong)CommentsBottomView*bottomView;
/**键盘上面评论输入框*/
@property (nonatomic,strong)PubulicInputToolbar*inputToolbar;

/**轮播图*/
@property (nonatomic,strong)SDCycleScrollView*headerView;

@end

@implementation HomeDeliciousFoodDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self addNavigationItemWithImageNames:@[@"sangedian1"] isLeft:NO target:self action:@selector(rightAction) tags:nil];
    
    [self.view addSubview:self.listView];
    [self.view addSubview:self.bottomView];
    
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.bottomView.mas_top);
    }];

    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
    }];
    [self.view addSubview:self.inputToolbar];


}
-(void)rightAction{
    PresentationObject * object = [PresentationObject sharedPresentationObject];
    object.subViewFrame = CGRectZero;
    [object showPresentingViewController:self PresentedViewController:[[BasseNavigationController alloc]initWithRootViewController:[ShareViewController new]]];

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBarBackgroundAndClearColor];
    [self setIQKeyboardManagerEnable:NO];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self setIQKeyboardManagerEnable:YES];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    DLogFunc;
    [self.view endEditing:YES];
}

#pragma mark --UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSoure.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    if (section == self.dataSoure.count - 1) {
        return 10;
    }
    NSArray * array = self.dataSoure[section];
    return array.count;
//    if (section == 1 || section == 2) {
//        return 2;
//    }
//
//    if (section == 3 ) {
//        return 3;
//    }
//    if (section == 4) {
//        return 4;
//    }
//    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1 || section == 2 || section == 3 || section == 4 || section == 5) {
        return 50;
    }

    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 50)];
        view.backgroundColor = KWhiteColor;
        UILabel * label = UILabel.new;
        if (section == 1) {
            label.text = @"主料";
        }
        if (section == 2) {
            label.text = @"辅料";
        }
        if (section == 3) {
            label.text = @"调料";
        }
        if (section == 4) {
            label.text = @"做法";
        }
        if (section == 5) {
            label.text = @"评论(20)";
        }

        label.textColor = UIColorFromRGB(0x273323);
        label.font = PingFangSC_Medium(18);

        [view addSubview:label];

        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.centerY.mas_equalTo(0);
        }];
        return view;

    return UIView.new;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return UIView.new;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeDeliciousFoodDetailOneCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeDeliciousFoodDetailOneCell class])];
    HomeDeliciousFoodDetailTwoCell * twoCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeDeliciousFoodDetailTwoCell class])];
    AnswerDetailCommentsCell * thereCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AnswerDetailCommentsCell class])];

    if (indexPath.section == 0) {
        cell.model = self.model;
        return cell;
    }
    if (indexPath.section == 5) {
        thereCell.authorLb.hidden = YES;
        thereCell.commenModel = [UserCommentsModel new];
        return thereCell;
    }
    NSArray * array = self.dataSoure[indexPath.section];
    twoCell.modelDic = array[indexPath.row];
    if (indexPath.row % 2 == 0) {
        twoCell.backgroundColor = UIColorFromRGB(0xF5F7F5);
    }else{
        twoCell.backgroundColor = UIColorFromRGB(0xFFFFFF);
    }
    twoCell.separatorInset = UIEdgeInsetsMake(0, KScreenWidth, 0, 0);
    return twoCell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSIndexPath * index = [NSIndexPath indexPathForRow:0 inSection:0];
    //标记的cell  在tableView中的坐标值
    CGRect  recttIntableview = [self.listView rectForRowAtIndexPath:index];
    //当前cell在屏幕中的坐标值
    CGRect rectInSuperView = [self.listView convertRect:recttIntableview toView:[self.listView superview]];
    
    DLog(@"滑动到%f",rectInSuperView.origin.y);
    
    if (rectInSuperView.origin.y < - K_TopHeight) {
        [self setNavBarBackgroundAndNotClearColor];
    }else{
        [self setNavBarBackgroundAndClearColor];
    }
    
}
#pragma mark 懒加载
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.tableFooterView = UIView.new;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;

        [_listView registerClass:[HomeDeliciousFoodDetailOneCell class] forCellReuseIdentifier:NSStringFromClass([HomeDeliciousFoodDetailOneCell class])];
        [_listView registerClass:[HomeDeliciousFoodDetailTwoCell class] forCellReuseIdentifier:NSStringFromClass([HomeDeliciousFoodDetailTwoCell class])];
        
        [_listView registerClass:[AnswerDetailCommentsCell class] forCellReuseIdentifier:NSStringFromClass([AnswerDetailCommentsCell class])];

        _listView.tableHeaderView = self.headerView;
        if (@available(iOS 11.0, *)) {
            _listView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _listView.separatorColor = SeparatorColor;

    }
    return _listView;
}
-(void)setModel:(HomeDeliciousFoodModel *)model{
    _model = model;
    if (model.seasoningArray.count > 0) {
        for (NSInteger i = 1; i < 5; i++) {
            NSArray * onearr = model.seasoningArray[i - 1];
            self.dataSoure[i] = onearr;
        }
        
        [self.listView reloadData];
        NSMutableArray * array = [NSMutableArray array];
        for (NSString * imageName in model.images) {
            NSString *aPath3=[NSString stringWithFormat:@"%@/Documents/%@.png",NSHomeDirectory(),imageName];
            [array addObject:aPath3];
        }
        self.headerView.imageURLStringsGroup = array;

    }
}
-(NSMutableArray *)dataSoure{
    if (!_dataSoure) {
        NSArray * array1 = @[@{@"leftName":@"豆腐",@"rightName":@"500克"},@{@"leftName":@"肉末",@"rightName":@"150克"}];
        NSArray * array2 = @[@{@"leftName":@"葱花",@"rightName":@"30克"},@{@"leftName":@"肉末",@"rightName":@"150克"}];
        NSArray * array3 = @[@{@"leftName":@"郫县豆瓣酱",@"rightName":@"30克"},@{@"leftName":@"花椒",@"rightName":@"150克"},@{@"leftName":@"麻椒",@"rightName":@"80克"}];
        NSArray * array4 = @[@{@"leftName":@"1.豆腐切一厘米见方的小块",@"rightName":@""},@{@"leftName":@"2.肉末剁好",@"rightName":@""},@{@"leftName":@"3.花椒和麻椒冷油下锅，慢火三分钟爆出香",@"rightName":@""},@{@"leftName":@"4.锅里底油放入蒜末和郫县豆瓣酱小火翻炒2分钟",@"rightName":@""}];
        _dataSoure =[NSMutableArray array];
        [_dataSoure addObject:@""];
        [_dataSoure addObject:array1];
        [_dataSoure addObject:array2];
        [_dataSoure addObject:array3];
        [_dataSoure addObject:array4];
        [_dataSoure addObject:@""];

    }
    return _dataSoure;
}

-(PubulicInputToolbar*)inputToolbar{
    if(!_inputToolbar){
        _inputToolbar=[[PubulicInputToolbar alloc] init];
        _inputToolbar.textViewMaxLine = 3;
        _inputToolbar.fontSize = 18;
        _inputToolbar.placeholder = @"写评论...";
        _inputToolbar.isTop = YES;
        kWeakSelf(weakSelf);
        [_inputToolbar inputToolbarSendText:^(NSString * _Nonnull text) {
            // 清空输入框文字
            [weakSelf.inputToolbar bounceToolbar];
        }];
    }
    return _inputToolbar;
}
-(CommentsBottomView*)bottomView{
    if(!_bottomView){
        _bottomView=[[CommentsBottomView alloc] initWithFrame:CGRectZero];
        kWeakSelf(weakSelf);
        [_bottomView settextFieldEnabled:^{
            [weakSelf.inputToolbar popToolbar];
        }];
    }
    return _bottomView;
}
-(SDCycleScrollView*)headerView{
    if(!_headerView){
        _headerView=[[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 210)];
        _headerView.imageURLStringsGroup = @[[Utilis getRandomDynamicImage],[Utilis getRandomDynamicImage],[Utilis getRandomDynamicImage],[Utilis getRandomDynamicImage]];
    }
    return _headerView;
}
@end

#import "UserIconAndNameView.h"

#pragma mark HomeDeliciousFoodDetailOneCell
@interface HomeDeliciousFoodDetailOneCell ()

/**用户视图*/
@property (nonatomic,strong)UserIconAndNameView*userView;
/**<#属性#>*/
@property (nonatomic,strong)UILabel * titleLabel;
/**<#属性#>*/
@property (nonatomic,strong)UILabel * contenText;
@end

@implementation HomeDeliciousFoodDetailOneCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
-(void)setModel:(HomeDeliciousFoodModel *)model{
    _model = model;
    if (model.title) {
        self.titleLabel.text = model.title;
        self.contenText.text = model.introduce;
    }
}
-(void)createUI{
    
    UIView * bgView = UIView.new;
//    bgView.backgroundColor = KRedColor;
    ViewRadius(bgView, 4);
    
//    bgView.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
//    bgView.layer.shadowColor = [UIColor colorWithRed:149/255.0 green:158/255.0 blue:151/255.0 alpha:0.25].CGColor;
//    bgView.layer.shadowOffset = CGSizeMake(0,1);
//    bgView.layer.shadowOpacity = 1;
//    bgView.layer.shadowRadius = 10;
    
    self.titleLabel= UILabel.new;
    self.titleLabel.font = PingFangSC_Medium(18);
    self.titleLabel.textColor = UIColorFromRGB(0x273323);
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.text = @"麻婆豆腐";
    
    self.contenText= UILabel.new;
    self.contenText.numberOfLines = 0;
    self.contenText.textColor = UIColorFromRGB(0x454F44);
    self.contenText.font = SYSTEMFONT(14);
    self.contenText.attributedText = [TitleTool getAttributeStringLineSpacing:4 title:@"豆腐的营养丰富，是家庭炒菜中广泛使用的材料。豆腐的做法很多，南北方各异，味道有清淡、有浓郁，亦可凉拌、烧煮、做汤。今天，做一款川味的豆腐，也是最平常的一种做法，那就是“麻婆豆腐”，这也是四川具有代表性的一款美食。" titleColor:UIColorFromRGB(0x454F44) titleFont:PingFangSC_Regular(14)];
    
    UIView * lineView = UIView.new;
    lineView.backgroundColor = SeparatorColor;
    
    [self addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(8);
        make.right.bottom.mas_equalTo(-8);
    }];
    
    [bgView addSubview:self.userView];
    [bgView addSubview:self.titleLabel];
    [bgView addSubview:self.contenText];
    
    [self.userView addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];

    [self.userView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.userView.mas_bottom);
        make.left.mas_equalTo(8);
        make.right.mas_equalTo(-8);
    }];
    [self.contenText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(16);
        make.bottom.mas_equalTo(-16);
    }];
    

}

#pragma mark 懒加载
-(UserIconAndNameView*)userView{
    if(!_userView){
        _userView=[[UserIconAndNameView alloc] initWithFrame:CGRectZero];
        _userView.dynamicData.text = @"美食达人";
        [_userView.deleButton setBackgroundImage:IMAGE_NAMED(@"guanzhu_bg") forState:UIControlStateNormal];
//        [_userView.deleButton setBackgroundImage:[UIImage createImageWithColor:<#(nullable UIColor *)#>] forState:UIControlStateSelected];
        [_userView.deleButton setTitle:@"关注" forState:UIControlStateNormal];
        [_userView.deleButton setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
        _userView.deleButton.titleLabel.font = SYSTEMFONT(14);
        [_userView.deleButton addActionHandler:^{
            
        }];

    }
    return _userView;
}

@end

@interface HomeDeliciousFoodDetailTwoCell()
/**名称*/
@property (nonatomic,strong)UILabel*leftLabel;
/**重量*/
@property (nonatomic,strong)UILabel*rightLabel;
@end

@implementation HomeDeliciousFoodDetailTwoCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self addSubview:self.leftLabel];
        [self addSubview:self.rightLabel];
        
        [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(16);
            make.bottom.right.mas_equalTo(-16);
            
        }];
        [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-16);
        }];

    }
    return self;
}
-(void)setModelDic:(NSDictionary *)modelDic{
    _modelDic = modelDic;
    self.leftLabel.text = modelDic[@"leftName"];
    self.rightLabel.text = modelDic[@"rightName"];

}
#pragma mark 懒加载

-(UILabel*)leftLabel{
    if(!_leftLabel){
        _leftLabel=[UILabel new];
        _leftLabel.textColor = UIColorFromRGB(0x454F44);
        _leftLabel.font = SYSTEMFONT(16);
        _leftLabel.numberOfLines=0;
        _leftLabel.text = @"豆腐";
    }
    return _leftLabel;
}
-(UILabel*)rightLabel{
    if(!_rightLabel){
        _rightLabel=[UILabel new];
        _rightLabel.textColor = UIColorFromRGB(0x454F44);
        _rightLabel.font = SYSTEMFONT(16);
        _rightLabel.text = @"500克";
    }
    return _rightLabel;
}

@end
